package com.linln.modules.tribe.service.impl;

import com.linln.common.data.PageSort;
import com.linln.common.enums.StatusEnum;
import com.linln.modules.tribe.domain.Tribetype;
import com.linln.modules.tribe.repository.TribetypeRepository;
import com.linln.modules.tribe.service.TribetypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 左书亮
 * @date 2019/10/10
 */
@Service
public class TribetypeServiceImpl implements TribetypeService {

    @Autowired
    private TribetypeRepository tribetypeRepository;

    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    @Transactional
    public Tribetype getById(Long id) {
        return tribetypeRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<Tribetype> getPageList(Example<Tribetype> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return tribetypeRepository.findAll(example, page);
    }

    @Override
	public Page<Tribetype> getPageList(Example<Tribetype> example,Integer pageNumber,Integer pageSize){
    	// 创建分页对象
   	 Sort sort = new Sort(Sort.Direction.DESC, "id");
        if(pageNumber==null) {
        	pageNumber=1;
        }    	
        
        PageRequest page = PageSort.pageRequest(pageNumber,pageSize,sort);

       return tribetypeRepository.findAll(example, page);
    }

    /**
     * 保存数据
     * @param tribetype 实体对象
     */
    @Override
    public Tribetype save(Tribetype tribetype) {
        return tribetypeRepository.save(tribetype);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return tribetypeRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }

	@Override
	public List<Tribetype> getListByExample(Example<Tribetype> example, Sort sort) {
		return tribetypeRepository.findAll(example, sort);
	}
}